package com.wyp168.leetcode;

/**
 * @ClassName MagicalStringSolution
 * @Title TODO
 * @Description TODO
 * @Author wyp168
 * @Date 2022/10/31 00:36
 */
public class MagicalStringSolution {
    public static void main(String[] args) {
        MagicalStringSolution solution = new MagicalStringSolution();
        System.out.println(solution.magicalString(6));

    }

    public int magicalString(int n) {
        if (n < 4) {
            return 1;
        }
        int[] arr = new int[n];
        int count = 1;
        arr[0] = 1;
        arr[1] = 2;
        arr[2] = 2;
        int condition = 2;
        int index = 3;
        int num = 1;

        while (index < n) {
            if (arr[condition] > 0) {
                if (num == 1) {
                    count ++;
                }
                arr[index] = num;
            }
            arr[condition] --;
            if (arr[condition] == 0) {
                condition ++;
                num = num == 1 ? 2 : 1;
            }
            index ++;
        }
        return count;
    }
}
